from unittest import TestCase, main

from webtest import TestApp

from clutch.wsgi import make_app_test as make_app

class TestViews(TestCase):

    def setUp(self):
        self.app = make_app({}, **{
            'use':'egg:Clutch',
            'clutch.app':'TestApplication#main',
            'login_url':'/login'})
        self.tester = TestApp(self.app)

    def testContentType(self):
        res = self.tester.get(
            '/tests/view/test1',
            headers=dict(Accept='application/json'))
        self.assert_('<html>' not in res.body)
        res = self.tester.get(
            '/tests/view/test1',
            headers=dict(Accept='text/html'))
        self.assert_('<html' in res.body, res.body)
        self.assert_('a = {{a}}' in res.body)
        self.assert_('a = 5' in res.body)
        res = self.tester.get(
            '/tests/view/test1',
            headers=dict(Accept='text/plain'))
        self.assert_('<html' in res.body)
        self.assert_('a = $a' in res.body)
        self.assert_('a = 5' in res.body)

    def testJinjaSandbox(self):
        from jinja2.sandbox import SecurityError
        self.assertRaises(SecurityError, self.tester.get, 
                          '/tests/view/test2')


